package indi.zn.commons.serialno;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Random;

import indi.zn.commons.collection.Maps;
import indi.zn.commons.easiness.Numbers;
import indi.zn.script.groovy.GroovyRunner;

/**
 *
 * 
 * @author 周楠
 * @Description:
 * @date 2017年10月13日
 *
 */
public class SerialNo {

	private String snClazz;
	private boolean gTime;
	private String bcode;
	private Integer randomBound;
	private Sequence seq;
	private Random random=new Random();
	
	private SerialExecutor executor;
	
	private DateFormat df=new SimpleDateFormat("yyyyMMdd");
	
	private Map<String, Object> basecontext; 
	
	
	private void initContext(Map<String,Object> extend) {
		basecontext=Maps.newMap();
		
		if(extend!=null) {
		basecontext.putAll(extend);
		}
		basecontext.put("bcode", bcode);
		
		
	}

	public SerialNo() {
		// TODO Auto-generated constructor stub
		initContext(null);
	}

	/**
	 * @param sn_clazz
	 * @param gTime
	 * @param bcode
	 * @param seq
	 * @param random
	 * @param formatter
	 */
	public SerialNo(String snClazz, boolean gTime, String bcode, Sequence seq,Integer randomBound, String formatter,GroovyRunner gRunner) {
		super();
		this.snClazz = snClazz;
		this.gTime = gTime;
		this.bcode = bcode;
		this.seq = seq;
		this.randomBound=randomBound;
		this.executor=new GroovyExecutor(snClazz,gRunner, formatter);
		
		initContext(null);
		
	}
	
	public SerialNo(String snClazz, boolean gTime, String bcode, Sequence seq,Integer randomBound, String formatter,GroovyRunner gRunner,DateFormat dateFormat) {
		super();
		this.snClazz = snClazz;
		this.gTime = gTime;
		this.bcode = bcode;
		this.seq = seq;
		this.randomBound=randomBound;
		this.executor=new GroovyExecutor(snClazz,gRunner, formatter);
		this.df=dateFormat;
		
		initContext(null);
		
	}

	public String getSnClazz() {
		return snClazz;
	}

	public void setSnClazz(String snClazz) {
		this.snClazz = snClazz;
	}

	public boolean isgTime() {
		return gTime;
	}

	public void setgTime(boolean gTime) {
		this.gTime = gTime;
	}

	public String getBcode() {
		return bcode;
	}

	public void setBcode(String bcode) {
		this.bcode = bcode;
	}

	public Sequence getSeq() {
		return seq;
	}

	public void setSeq(Sequence seq) {
		this.seq = seq;
	}

	public Random getRandom() {
		return random;
	}

	public void setRandom(Random random) {
		this.random = random;
	}


	public String getFormatterSerialNo() {
		
		
		Map<String, Object> bindMap=Maps.<String, Object>newMap(basecontext);
	
		bindMap.put("random", random());
		
		if (gTime) {
			bindMap.put("time", df.format(new Date()));
		}
		bindMap.put("seq", seq.getNext().toString());
		
		String sn=  executor.execute(bindMap);
		return sn;
	}
	
	
	public String getFormatterSerialNo(Map<String, ?>  parms) {
		
		Map<String, Object> bindMap=Maps.<String, Object>newMap(basecontext);
		
		

		bindMap.put("random", random());
	
		
		if (gTime) {
			bindMap.put("time", df.format(new Date()));
	
		}
		bindMap.put("seq", seq.getNext().toString());
		
		bindMap.putAll(parms);
		
		String sn=  executor.execute(bindMap);
		return sn;
	}


	 public String random() {
		 
		 String  randomInt="";
		 
		 if(randomBound!=null) {
				
				int bdig=Numbers.digit(randomBound);
				
				int ri=random.nextInt(randomBound);
					
				int ridig=Numbers.digit(ri);
				
				int c=bdig-ridig;
				
				if(c>0) {
					
				for (int i = 0; i < c; i++) {
					
					randomInt+="0";
					
				}
				
					
				}
					
					randomInt+=ri;
				
				
		 }
		return randomInt;
		  
	 }
	
	
	

	public String getSerialNoStr() {

		String sn = bcode;
		String time = "";

		if (gTime) {
			Calendar c = Calendar.getInstance();
			time = "" + c.get(Calendar.YEAR) + (c.get(Calendar.MONTH) + 1) + c.get(Calendar.DAY_OF_MONTH);
		}

		sn += time + seq.getNext().toString();
		
		return sn;

	}

	public String getSerialNoStr(String attachCode) {

		String sn = bcode + attachCode;
		String time = "";

		if (gTime) {
			Calendar c = Calendar.getInstance();
			time = "" + c.get(Calendar.YEAR) + (c.get(Calendar.MONTH) + 1) + c.get(Calendar.DAY_OF_MONTH);
		}

		sn += time + seq.getNext().toString();

		return sn;

	}

	public void clearSeq() {

		seq.reset();

	}

	


}
